<?php
/**
 * Mechanism
 *
 * LICENSE
 *
 * Copyright (c) 2011, Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * * Neither the name of the  nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @package Mechanism
 * @license http://www.opensource.org/licenses/bsd-license
 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
 * @copyright Copyright (c) 2011 Sergey A. Ivonchik
 */

namespace Mechanism {
	/**
	 * MechanicalTest
	 *
	 * Mechanical test case
	 *
 	 * @package Mechanism
 	 * @author Sergey A. Ivonchik <BreatheInMyVoid@gmail.com>
	 */
	class MechanicalTest extends \PHPUnit_Framework_TestCase {
		/**
		 * Test for constructor of an object
		 */
		public function testConstructor( ) {
			$mech = new Mechanical( new Test\Dummy( ) );

			try {
				$bad_mech = new Mechanical( 'bad_value' );
			} catch ( Exception\Invalid $e ) {
				return;
			}

			$this->fail('An expected Invalid exception has not been raised.');
		}

		/**
		 * Test method for add_gear
		 *
		 * @return Mechanical $mech
		 */
		public function testAddGear( ) {
			$gear = new Test\Gear( 'gear' );

			$mech = new Mechanical( new Test\Dummy( ) );

			/* Test for method and fluent interface of it */
			$this->assertSame( 
				$mech,
				$mech->add_gear( $gear ),
				'add_gear has not fluent interface'
			);

			return $mech;
		}

		/**
		 * Test method for get_gear
		 *
		 * @depends testAddGear
		 * @param Mechanical $mech
		 */
		public function testGetGear( IMechanical $mech ) {
			$gear = $mech->get_gear( 'gear' );

			$this->assertNotNull(
				$gear,
				'Method get_gear fails getting gear by name'
			);

			$this->assertTrue(
				$gear instanceof Test\Gear,
				'Gear is not instance of Test\Gear'
			);
		}

		/**
		 * Test method for get_gears
		 */
		public function testGetGears( ) {
			$gear_one = new Test\Gear( 'gear_one' );
			$gear_two = new Test\Gear( 'gear_two' );

			$mech = new Mechanical( new Test\Dummy( ) );
			$mech->add_gear( $gear_one );
			$mech->add_gear( $gear_two );

			$gears = $mech->get_gears( );

			/* Test size */
			$this->assertEquals(
				2,
				count( $gears ),
				'Gears size not equal'
			);

			/* Test types */
			$this->containsOnly(
				'object',
				$gears,
				'Bad type of gears'
			);
		}

		/**
		 * Test method for unmechanize
		 */
		public function testUnmechanize( ) {
			$unmech = new Test\Dummy( );
			$mech = new Mechanical( $unmech );

			$this->assertSame(
				$unmech,
				$mech->unmechanize( ),
				'Can\'t unmechanize object'
			);
		}

		/**
		 * Test set overloading
		 *
		 * @return Mechanical
		 */
		public function testSet( ) {
			$mech = new Mechanical( new Test\Dummy( ) );
			$mech->setted_var = 1;

			$this->assertObjectHasAttribute(
				'setted_var',
				$mech->unmechanize( ),
				'Set overloading does not work'
			);

			return $mech;
		}

		/**
		 * Test get overloading
		 *
		 * @depends testSet
		 * @param Mechanical
		 */
		public function testGet( IMechanical $mech ) {
			$setted_var = $mech->setted_var;
			
			$this->assertEquals(
				1,
				$setted_var,
				'Get overloading does not work'
			);
		}
		
		/**
		 * Test for main functionality of an object
		 *
		 * @uses Test\Gear
		 * @uses Test\Dummy
		 */
		public function testMechanical( ) {
			$mech = new Mechanical( new Test\Dummy( ) );
			$mech->add_gear( new Test\Gear( ) );

			/* Test for methods without gear */
			$mech->set_var( 1 );

			$this->assertEquals( 
				1,
				$mech->get_var( ),
				'Dummy values not equal'
			);

			/* Just enshure that gear methods has not been called */
			$this->assertObjectNotHasAttribute(
				'gear_var',
				$mech->unmechanize( ),
				'Gear method was called'
			);
			
			/* Test for method with gear */
			$mech->method();

			$this->assertObjectHasAttribute(
				'gear_var_one',
				$mech->unmechanize( ),
				'Gear pre_method has not been called'
			);
			$this->assertObjectHasAttribute(
				'gear_var_two',
				$mech->unmechanize( ),
				'Gear post_method has not been called' 
			);
			$this->assertObjectNotHasAttribute(
				'unexsistence',
				$mech->unmechanize( ),
				'Gear post_method has not been called'
			);

			/* Test two gears */
			$mech->add_gear( new Test\Gear( ) );

			$this->assertObjectNotHasAttribute(
				'var_two',
				$mech->unmechanize( )
			);

			$mech->sec_method();
			
			$this->assertEquals(
				2,
				$mech->unmechanize( )->var_two,
				'Wrong work of two gears'
			);
		}
	}
}
